<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-input v-if="this.userType === 1" @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="平台编号" v-model="listQuery.platformId"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="用户编号" v-model="listQuery.customerId"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="个人客户编号" v-model="listQuery.personalId"> </el-input>
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
    <el-button class="filter-item"  v-if="personalManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column width="160" align="center" label="个人客户编号"> <template scope="scope">
          <span>{{scope.row.personalId}}</span>
        </template> </el-table-column>
    <el-table-column width="110" align="center" label="平台编号"> <template scope="scope">
        <span>{{scope.row.platformId}}</span>
      </template> </el-table-column>
    <el-table-column width="110" align="center" label="用户编号"> <template scope="scope">
            <span>{{scope.row.customerId}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="归属人"> <template scope="scope">
            <span>{{scope.row.owner}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="第三方会员编号"> <template scope="scope">
            <span>{{scope.row.thirdNo}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="用户名称"> <template scope="scope">
            <span>{{scope.row.realName}}</span>
          </template> </el-table-column>
    <el-table-column width="100" align="center" label="身份证号"> <template scope="scope">
            <span>{{scope.row.idNumber}}</span>
          </template> </el-table-column>
    <el-table-column width="100" align="center" label="用户昵称"> <template scope="scope">
            <span>{{scope.row.nickName}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="性别"> <template scope="scope">
            <span v-if="scope.row.sex === '1'">男</span>
            <span v-if="scope.row.sex === '2'">女</span>
          </template> </el-table-column>
    <el-table-column width="180" align="center" label="生日"> <template scope="scope">
          <span>{{scope.row.birthday}}</span>
        </template> </el-table-column>
    <el-table-column width="110" align="center" label="居住地"> <template scope="scope">
            <span>{{scope.row.address}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="家乡"> <template scope="scope">
            <span>{{scope.row.hometown}}</span>
          </template> </el-table-column>
    <el-table-column width="160" align="center" label="身份证正面"> <template scope="scope">
            <span>{{scope.row.idPositive}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="身份证反面"> <template scope="scope">
            <span>{{scope.row.idNegative}}</span>
          </template> </el-table-column>
    <el-table-column width="160" align="center" label="认证状态"> <template scope="scope">
            <span v-if="scope.row.authenticationStatus === '00'">未认证</span>
            <span v-if="scope.row.authenticationStatus === '01'">无需认证</span>
            <span v-if="scope.row.authenticationStatus === '02'">认证成功</span>
            <span v-if="scope.row.authenticationStatus === '03'">认证失败</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="账户状态"> <template scope="scope">
            <span v-if="scope.row.status === '00'">正常</span>
            <span v-if="scope.row.status === '01'">冻结</span>
          </template> </el-table-column>
    <el-table-column width="160" align="center" label="创建时间"> <template scope="scope">
            <span>{{scope.row.createTime}}</span>
          </template> </el-table-column>
    <el-table-column width="160" align="center" label="更新时间"> <template scope="scope">
            <span>{{scope.row.updateTime}}</span>
          </template> </el-table-column>
    <el-table-column align="center" label="操作" width="300"> <template scope="scope">
        <el-button v-if="personalManager_btn_update" size="small" type="success" @click="handleUpdate(scope.row)">编辑</el-button>
        <el-button v-if="personalManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="用户名称" :label-width="formLabelWidth" prop="realName">
        <el-input style="width:200px;" v-model="form.realName" placeholder="请输入用户名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="身份证号" :label-width="formLabelWidth" prop="idNumber">
        <el-input style="width:200px;" v-model="form.idNumber" placeholder="请输入身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" :label-width="formLabelWidth" prop="nickName">
        <el-input style="width:200px;" v-model="form.nickName" placeholder="请输入用户昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option v-for="item in sexOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生日" :label-width="formLabelWidth" prop="birthday">
        <el-date-picker v-model="form.birthday" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="居住地" :label-width="formLabelWidth" prop="address">
        <el-input style="width:200px;" v-model="form.address" placeholder="请输入居住地" clearable></el-input>
      </el-form-item>
      <el-form-item label="家乡" :label-width="formLabelWidth" prop="hometown">
        <el-input style="width:200px;" v-model="form.hometown" placeholder="请输入家乡" clearable></el-input>
      </el-form-item>
      <el-form-item label="身份证正面" :label-width="formLabelWidth" prop="idPositive">
        <div class="imgSrc" @click="upIdPositiveUrl">
          <img v-if="form.idPositive" :src="form.idPositive" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </div>
        <input type="file" name="file" id="upload_idPositive" @change="upIdPositive" accept="image/png,image/gif" style="display: none">
      </el-form-item>
      <el-form-item label="身份证反面" :label-width="formLabelWidth" prop="idNegative">
        <div class="imgSrc" @click="upIdNegativeUrl">
          <img v-if="form.idNegative" :src="form.idNegative" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </div>
        <input type="file" name="file" id="upload_idNegative" @change="upIdNegative" accept="image/png,image/gif" style="display: none">
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>

</div>
</template>

<script>
import axios from 'axios';
import { page, getObj, addObj, putObj, delObj } from '@/api/crm/customers/personalBase'
import { mapGetters } from 'vuex'
export default {
  name: 'personals',
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        platformId: undefined,
        customerId: undefined,
        personalId: undefined
      },
      form: {},
      list: null,
      selectTypeOptions: [],
      selectStatusOptions: [],
      total: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      personalManager_btn_add: false,
      personalManager_btn_update: false,
      personalManager_btn_del: false,
      textMap: {
        create: '添加',
        update: '修改'
      },
      sexOptions: [{
          value: '1',
          label: '男'
        }, {
          value: '2',
          label: '女'
        }],
      tableKey: 0,
      formLabelWidth: '120px',
      rules: {
        realName: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        idNumber: [
          {
            required: true,
            message: '请输入身份证号',
            trigger: 'blur'
          },
          {
            min: 15,
            max: 18,
            message: '长度在 15 到 18 个字符',
            trigger: 'blur'
          }
        ],
        nickName: [
          {
            required: true,
            message: '请输入昵称',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 20,
            message: '长度在 5 到 20 个字符',
            trigger: 'blur'
          }
        ],
        sex: [
          {
            required: true,
            message: '请选择性别',
            trigger: 'blur'
          }
        ],
        birthday: [
          {
            required: true,
            message: '请选择生日',
            trigger: 'blur'
          }
        ],
        address: [
          {
            required: true,
            message: '请输入居住地',
            trigger: 'blur'
          }
        ],
        hometown: [
          {
            required: true,
            message: '请输入家乡',
            trigger: 'blur'
          }
        // ],
        // idPositive: [
        //   {
        //     required: true,
        //     message: '请上传身份证正面',
        //     trigger: 'blur'
        //   }
        // ],
        // idNegative: [
        //   {
        //     required: true,
        //     message: '请上传身份证反面',
        //     trigger: 'blur'
        //   }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.personalManager_btn_add = this.elements['personalManager:btn_add']
    this.personalManager_btn_update = this.elements['personalManager:btn_update']
    this.personalManager_btn_del = this.elements['personalManager:btn_del']
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'userType'
    ])
  },
  methods: {
    getList() {
      this.listLoading = true
      page(this.listQuery)
        .then(response => {
          this.list = response.body.data.rows
          this.total = response.body.data.total
          this.listLoading = false
        })
    },
    handleCreate() {
      this.form = {}
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      getObj(row.id)
        .then(response => {
          this.form = response.body.data
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row.id)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
        })
    },
    create(formName) {
      const set = this.$refs
      console.log('addObj=======================================================')
      console.log(this.form)
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form)
            .then(response => {
              if (response.body.code === '0000') {
                this.$notify({
                  title: '成功',
                  message: '添加成功',
                  type: 'success',
                  duration: 2000
                })
              } else {
                this.$notify({
                  title: '失败',
                  message: '添加失败',
                  type: 'fail',
                  duration: 2000
                })
              }
              this.dialogFormVisible = false
              this.getList()
            })
        } else {
          return false
        }
      })
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          putObj(this.form.id,this.form).then(response => {
            if (response.body.code === '0000') {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify({
                title: '失败',
                message: '修改失败',
                type: 'fail',
                duration: 2000
              })
            }
            this.dialogFormVisible = false
            this.getList()
          })
        } else {
          return false
        }
      })
    },


    handleFilter() {
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    // 身份证正面
    upIdPositiveUrl: function() {
      document.getElementById('upload_idPositive').click()
    },
    upIdPositive(event){
      if(event.target.files.length>0){
          this.file = event.target.files[0];  //提交的图片
          console.log(this.file)
          let param = new FormData() // 创建form对象
          param.append('file', this.file)
          const instance=axios.create({
            baseURL: process.env.BASE_API, // api的base_url
            withCredentials: true
          })
          let config = {
            headers: {'Content-Type': 'multipart/form-data','Authorization':getToken()}
          }
          instance.post('/api/crm/upload/file', param, config)
          .then(response => {
            console.log(response.body.data)
            this.form.idPositive = response.body.data.filePath
          })
      }
    },
    // 身份证反面
    upIdNegativeUrl: function() {
      document.getElementById('upload_idNegative').click()
    },
    upIdNegative(event){
      if(event.target.files.length>0){
          this.file = event.target.files[0];  //提交的图片
          console.log(this.file)
          let param = new FormData() // 创建form对象
          param.append('file', this.file)
          const instance=axios.create({
            withCredentials: true
          })
          let config = {
            headers: {'Content-Type': 'multipart/form-data','Authorization':getToken()}
          }
          instance.post('/api/crm/upload/file', param, config)
          .then(response => {
            console.log(response.body.data)
            this.form.idNegative = response.body.data.filePath
          })
      }
    }
  }
}
</script>
<style scoped>
.imgSrc {
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
</style>
